package com.ruoyi.company.utils.pdf;

import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.ruoyi.company.utils.Util;
import org.springframework.security.core.parameters.P;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * @author WangChangShuo
 * @date 2022年04月29日 9:30
 */
public class ImgToPdf {
    public static void main(String[] args) throws Exception {

        String contractTerms = "一、交货时间：乙方收到货款后，当日或第二个工作日发货。\n" +
                "二、运输及费用承担方式:物流送货至购货单位指定地址，供货方承担运费。\n" +
                "三、货款结算方式:购货方以电汇方式汇款，款到发货。开具13%增值税专用发票。\n" +
                "四、质保与违约责任∶缅为生产企业原厂出品，原装进口，并符合中国医疗器械管理政策，否则无条件退货退款。\n" +
                "五、质声验收时效:2个工作日内验收。本协议法律争议管辖地为山东省济南市。\n" +
                "六、合同一式两份，双方签字盖章后生效。扫描件具有同等法律效力。";

        //根据回车进行分割
        String[] contractTermsArr = contractTerms.split("\\r?\\n");

        for (int i = 0; i < contractTermsArr.length; i++) {
            //每行108个字符
            String[] strings = Util.stringToArray(contractTermsArr[i]);
            String interception = Util.interception(strings, 81);
            //根据回车进行分割
            String[] split = interception.split("换行aaaaa");
            for (int j = 0; j < split.length; j++) {
                String s = split[j];
                System.out.println(s);
            }
        }
    }

    /**
     * 将图片放到pdf上(pdf左侧)
     *
     * @param pdfPath   pdf路径
     * @param newPath   新存放路径
     * @param imgPath   图片路径
     * @param sbsoluteY 图片水平位置
     * @throws Exception
     */
    public static void imgToPdfLeft(String pdfPath, String newPath, String imgPath, int sbsoluteY) throws Exception {
        //pdf文件存放在E盘
        File file = new File(pdfPath);
        //合成后的文件
        FileOutputStream outputStream = null;
        outputStream = new FileOutputStream(newPath);
        PdfReader reader = null;
        reader = new PdfReader(new FileInputStream(file));
        PdfStamper stamper = new PdfStamper(reader, outputStream);
        //将签名图片放在pdf文件的第1页
        PdfContentByte over = stamper.getOverContent(1);
        //签名图片
        Image contractSealImg = Image.getInstance(imgPath);
        over.saveState();
        PdfGState pdfGState = new PdfGState();
        pdfGState.setFillOpacity(0.8F);  //给图片设置透明度
        over.setGState(pdfGState);
        contractSealImg.setAbsolutePosition(120, sbsoluteY);  //设置图片位置
        contractSealImg.scaleAbsolute(130, 130);  //设置图片大小
        over.addImage(contractSealImg);  //将图片添加到pdf文件
        over.restoreState();
        stamper.setFormFlattening(true);
        stamper.close();
        reader.close();
        outputStream.close();
    }


    /**
     * 将图片放到pdf上(pdf右侧)
     *
     * @param pdfPath   pdf路径
     * @param newPath   新存放路径
     * @param imgPath   图片路径
     * @param sbsoluteY 图片水平位置
     * @throws Exception
     */
    public static void imgToPdfRight(String pdfPath, String newPath, String imgPath, int sbsoluteY) throws Exception {
        //pdf文件存放在
        File file = new File(pdfPath);
        //合成后的文件
        FileOutputStream outputStream = new FileOutputStream(newPath);
        PdfReader reader = new PdfReader(new FileInputStream(file));
        PdfStamper stamper = new PdfStamper(reader, outputStream);
        //将签名图片放在pdf文件的第1页
        PdfContentByte over = stamper.getOverContent(1);
        //签名图片
        Image contractSealImg = Image.getInstance(imgPath);
        over.saveState();
        PdfGState pdfGState = new PdfGState();
        pdfGState.setFillOpacity(0.8F);  //给图片设置透明度
        over.setGState(pdfGState);
        contractSealImg.setAbsolutePosition(350, sbsoluteY);  //设置图片位置
        contractSealImg.scaleAbsolute(130, 130);  //设置图片大小
        over.addImage(contractSealImg);  //将图片添加到pdf文件
        over.restoreState();
        stamper.setFormFlattening(true);
        stamper.close();
        reader.close();
        outputStream.close();
    }

    public static void imgToPdfRightSave(String pdfPath, String newPath, String imgPath, int sbsoluteY) throws Exception {
        //pdf文件存放在
        File file = new File(pdfPath);
        //合成后的文件
        FileOutputStream outputStream = new FileOutputStream(newPath);
        PdfReader reader = new PdfReader(new FileInputStream(file));
        PdfStamper stamper = new PdfStamper(reader, outputStream);
        //将签名图片放在pdf文件的第1页
        PdfContentByte over = stamper.getOverContent(1);
        //签名图片
        Image contractSealImg = Image.getInstance(imgPath);
        over.saveState();
        PdfGState pdfGState = new PdfGState();
        pdfGState.setFillOpacity(0.8F);  //给图片设置透明度
        over.setGState(pdfGState);
        contractSealImg.setAbsolutePosition(400, sbsoluteY);  //设置图片位置
        contractSealImg.scaleAbsolute(130, 130);  //设置图片大小
        over.addImage(contractSealImg);  //将图片添加到pdf文件
        over.restoreState();
        stamper.setFormFlattening(true);
        stamper.close();
        reader.close();
        outputStream.close();
    }




    public static void imgAuthorizationPdfRight(String pdfPath, String newPath, String imgPath, String zmPhoto, String fmPhoto, int sbsoluteY) throws Exception {
        //pdf文件存放在
        File file = new File(pdfPath);
        //合成后的文件
        FileOutputStream outputStream = new FileOutputStream(newPath);
        PdfReader reader = new PdfReader(new FileInputStream(file));
        PdfStamper stamper = new PdfStamper(reader, outputStream);
        //将签名图片放在pdf文件的第1页
        PdfContentByte over = stamper.getOverContent(1);
        //签名图片
        Image contractSealImg = Image.getInstance(imgPath);
        over.saveState();
        PdfGState pdfGState = new PdfGState();
        pdfGState.setFillOpacity(0.8F);  //给图片设置透明度
        over.setGState(pdfGState);
        contractSealImg.setAbsolutePosition(350, sbsoluteY);  //设置图片位置
        contractSealImg.scaleAbsolute(130, 130);  //设置图片大小
        over.addImage(contractSealImg);  //将图片添加到pdf文件
        over.restoreState();

        //身份证正面
        PdfContentByte over2 = stamper.getOverContent(1);
        Image contractSealImg1 = Image.getInstance(zmPhoto);
        over2.saveState();
        PdfGState pdfGState1 = new PdfGState();
        pdfGState1.setFillOpacity(1.0F);  //给图片设置透明度
        over2.setGState(pdfGState1);
        contractSealImg1.setAbsolutePosition(20, 200);  //设置图片位置
        contractSealImg1.scaleAbsolute(240, 180);  //设置图片大小
        over2.addImage(contractSealImg1);  //将图片添加到pdf文件
        over2.restoreState();

        //身份证反面

        PdfContentByte over3 = stamper.getOverContent(1);
        Image contractSealImg2 = Image.getInstance(fmPhoto);
        over3.saveState();
        ;
        PdfGState pdfGState2 = new PdfGState();
        over3.setGState(pdfGState2);
        contractSealImg2.setAbsolutePosition(310, 200);
        contractSealImg2.scaleAbsolute(240, 180);
        over3.addImage(contractSealImg2);
        over3.restoreState();

        stamper.setFormFlattening(true);
        stamper.close();
        reader.close();
        outputStream.close();
    }


    public static void saleImgToPdfLeft(String pdfPath, String newPath, String frqz, String imgPath, int sbsoluteY) throws Exception {
        //pdf文件存放在E盘
        File file = new File(pdfPath);
        //合成后的文件
        FileOutputStream outputStream = null;
        outputStream = new FileOutputStream(newPath);
        PdfReader reader = null;
        reader = new PdfReader(new FileInputStream(file));
        PdfStamper stamper = new PdfStamper(reader, outputStream);
        //将签名图片放在pdf文件的第1页
        PdfContentByte over = stamper.getOverContent(1);
        //签名图片
        Image contractSealImg = Image.getInstance(imgPath);
        over.saveState();
        PdfGState pdfGState = new PdfGState();
        pdfGState.setFillOpacity(0.8F);  //给图片设置透明度
        over.setGState(pdfGState);
        contractSealImg.setAbsolutePosition(120, sbsoluteY);  //设置图片位置
        contractSealImg.scaleAbsolute(130, 130);  //设置图片大小
        over.addImage(contractSealImg);  //将图片添加到pdf文件
        over.restoreState();

        PdfContentByte over2 = stamper.getOverContent(1);
        Image contractSealImg1 = Image.getInstance(frqz);
        over2.saveState();
        PdfGState pdfGState1 = new PdfGState();
        pdfGState1.setFillOpacity(1.0F);  //给图片设置透明度
        over2.setGState(pdfGState1);
        contractSealImg1.setAbsolutePosition(120, 300);  //设置图片位置
        contractSealImg1.scaleAbsolute(130, 130);  //设置图片大小
        over2.addImage(contractSealImg1);  //将图片添加到pdf文件
        over2.restoreState();


        stamper.setFormFlattening(true);
        stamper.close();
        reader.close();
        outputStream.close();
    }

    /**
     * 将图片从pdf上移除
     *
     * @param pdfPath pdf路径
     * @param newPath 新存放路径
     * @throws Exception
     */
    public static void imgToPdf(String pdfPath, String newPath) throws Exception {
        //pdf文件存放在
        File file = new File(pdfPath);
        //合成后的文件
        FileOutputStream outputStream = new FileOutputStream(newPath);
        PdfReader reader = new PdfReader(new FileInputStream(file));
        PdfStamper stamper = new PdfStamper(reader, outputStream);
        stamper.setFormFlattening(true);
        stamper.close();
        reader.close();
        outputStream.close();
    }
}
